package com.example.gateway.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springdoc.core.models.GroupedOpenApi;
import org.springdoc.core.properties.SwaggerUiConfigParameters;
import org.springdoc.core.properties.SwaggerUiConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class SwaggerConfig {

    @Autowired
    private SwaggerUiConfigProperties swaggerUiConfigProperties;

    @Bean
    public CommandLineRunner swaggerConfigRunner() {
        return args -> {
            // 确保默认选择聚合API文档
            swaggerUiConfigProperties.setUrlsPrimaryName("聚合API文档");
            // 设置模型扩展深度
            swaggerUiConfigProperties.setDefaultModelExpandDepth(3);
            swaggerUiConfigProperties.setDefaultModelsExpandDepth(3);
            // 设置文档默认展开级别 (list, full, none)
            swaggerUiConfigProperties.setDocExpansion("list");
            // 启用请求持续时间显示
            swaggerUiConfigProperties.setDisplayRequestDuration(true);
            // 禁用默认URL
            swaggerUiConfigProperties.setDisableSwaggerDefaultUrl(true);
        };
    }

    @Bean
    @Primary
    public OpenAPI gatewayOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("酒店管理系统API")
                        .description("基于微服务架构的酒店管理系统API文档")
                        .version("v1.0")
                        .contact(new Contact()
                                .name("MISP0306")
                                .email("331617682@qq.com"))
                        .license(new License()
                                .name("Apache 2.0")
                                .url("http://www.apache.org/licenses/LICENSE-2.0"))
                );
    }

    @Bean
    public GroupedOpenApi allApi() {
        return GroupedOpenApi.builder()
                .group("all-api")
                .pathsToMatch("/**")
                .build();
    }

    @Bean
    public GroupedOpenApi roomTypeApi() {
        return GroupedOpenApi.builder()
                .group("room-type-api")
                .pathsToMatch("/roomType/**", "/priceRule/**")
                .build();
    }

    @Bean
    public GroupedOpenApi roomApi() {
        return GroupedOpenApi.builder()
                .group("room-api")
                .pathsToMatch("/room/**")
                .build();
    }

    @Bean
    public GroupedOpenApi checkInApi() {
        return GroupedOpenApi.builder()
                .group("check-in-api")
                .pathsToMatch("/checkIn/**")
                .build();
    }

    @Bean
    public GroupedOpenApi paymentApi() {
        return GroupedOpenApi.builder()
                .group("payment-api")
                .pathsToMatch("/payment/**")
                .build();
    }

    @Bean
    public GroupedOpenApi userApi() {
        return GroupedOpenApi.builder()
                .group("user-api")
                .pathsToMatch("/user/**")
                .build();
    }

    @Bean
    public GroupedOpenApi ossApi() {
        return GroupedOpenApi.builder()
                .group("oss-api")
                .pathsToMatch("/oss/**")
                .build();
    }
} 